#!/bin/bash

mount_dir_list=('/dockerData/000' '/dockerData/001' '/dockerData/002')
disk_name_base_list=('vdc' 'vdd' 'vde' )

for (( i=0; i < ${#mount_dir_list[*]}; i++ )); do

    disk_name_base=${disk_name_base_list[i]}
    disk_name='/dev/'$disk_name_base
    disk_p='/dev/'$disk_name_base'1'
    find_key='\/dev\/'$disk_name_base'1'
	
	mount_dir=${mount_dir_list[i]}


    ret_disk=`fdisk -l ${disk_name} |sed '/^$/d'|sed -n '$p' | awk '{print $1}'`
    #“#”是gpt格式的没有分区；“Device”是mbr（msdos）没有分区, 没有分区，执行分区和格式化；否则忽略
    if [ "${ret_disk}"x = "#"x ] || [ "${ret_disk}"x = "Device"x ] || [ "${ret_disk}"x = "I/O"x ] || [ "${ret_disk}"x = "Disk"x ];
    then
        #1、分区
        echo "n
            p
            1
            

            w
            " | fdisk ${disk_name}
	    #2、格式化
	    partprobe
	    mkfs.ext4 ${disk_p}
    else
    	echo "Data disk has been partitioned."
    fi


    #判断是否已经挂载
    ret_is_mounted=`df -lh |grep ${disk_p}|grep ${mount_dir}|wc -l`
    if [ ${ret_is_mounted} -eq 1 ]
    then
    	echo "Data disk has been mounted."
    	echo "init data disk success."	
    else
    	#没有挂载
	    #3、挂载 &&开机自动挂载
	    config_file='/etc/fstab'
	    mkfs.ext4 ${disk_p} && mkdir -p ${mount_dir} && mount ${disk_p} ${mount_dir}  && echo "init data disk success." && sed -i "/${find_key}/d"  $config_file && echo "${disk_p}  ${mount_dir} ext4 defaults  0  0" >> $config_file
    fi

done


